package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetaillnfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
/**
 * RestController注解：
 * 1.组合注解，相当于 Controer + ResponseBody 的组合；
 * 2.在类上添加该注解后，所有的控制器方法上无需再加@ResponseBody 注解了；
 */
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    private final NoticeMapper noticeMapper;

    public NoticeController(NoticeMapper noticeMapper) {
        this.noticeMapper = noticeMapper;
    }

    /**1.添加功能*/
    @PostMapping("add")

    public JsonResult add(NoticeAddParam noticeAddParam){
        System.out.println("noticeAddParm = " + noticeAddParam);
        //将咨询信息插入到数据表中[notice]
        Notice notice = new Notice();

        //复制属性
        BeanUtils.copyProperties(noticeAddParam, notice);
        //处理其他属性
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        noticeMapper.insertNotice(notice);
        return new JsonResult(1000,"添加咨询成功",null);
    }

    /**咨询列表功能*/
    @GetMapping("list")
    public JsonResult list(NoticeAddQuery noticeAddQuery){
        System.out.println("noticeAddQuery = " + noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNotice(title,type);
        return new JsonResult(2000,"查询资讯列表成功",noticeListVOS);
    }

    /**3.咨询详情功能*/
   @GetMapping("detail")
    public JsonResult detail(Long id){
       System.out.println("id = " + id);
       NoticeDetaillnfoVO noticeDetaillnfoVO = noticeMapper.selectById(id);
       return new JsonResult(3000,"咨询详情成功",noticeDetaillnfoVO);
   }
   /**4，删除资讯*/
   @DeleteMapping("del")

    public JsonResult del(Integer id){
       System.out.println("id = " + id);
       int i = noticeMapper.deleteById(id);
       if(i>0){
           return new JsonResult(4001,"删除资讯成功",null);
       }
       return new JsonResult(4001,"删除资讯成功",null);
   }

   /**5,更新资讯功能*/
   @PostMapping("update")

    public JsonResult update(NoticeUpdateParam noticeUpdateParam){
       System.out.println("noticeUpdateParam = " + noticeUpdateParam);
       //更新资讯使用实体类
       Notice notice = new Notice();
       BeanUtils.copyProperties(noticeUpdateParam, notice);
       //设置更新时间
       notice.setUpdateTime(new Date());
       //调用接口
       int i = noticeMapper.updateNotice(notice);
        if(i>0){
            return new JsonResult(5001,"更新资源成功",null);
        }
       return new JsonResult(5002,"更新资源失败",null);
   }

}
